package com.bignerdranch.android.furnitureapplication;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

import com.ashokvarma.bottomnavigation.BottomNavigationBar;
import com.ashokvarma.bottomnavigation.BottomNavigationItem;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity implements BottomNavigationBar.OnTabSelectedListener {

    private BottomNavigationBar mBottomNavigationBar;
    private MainFragment mMainFragment;
    private AccountFragment mAccountFragment;
    private ArrayList<Fragment> mFragments;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initViews();
    }

    private void initViews() {
        mBottomNavigationBar = findViewById(R.id.main_bottom);
        mBottomNavigationBar.setMode(BottomNavigationBar.MODE_FIXED);
        mBottomNavigationBar.setBackgroundStyle(BottomNavigationBar.BACKGROUND_STYLE_STATIC);

        mBottomNavigationBar
                .addItem(new BottomNavigationItem(R.mipmap.ic_home_true, "首页").setInactiveIconResource(R.mipmap.ic_home_false).setActiveColor("#F44336").setInActiveColor("#848484"))
                .addItem(new BottomNavigationItem(R.mipmap.ic_user_true, "我的").setInactiveIconResource(R.mipmap.ic_user_false).setActiveColor("#F44336").setInActiveColor("#848484"))
                .setFirstSelectedPosition(0)
                .initialise();
        mFragments = getFragments();
        setDefaultFragment();
        mBottomNavigationBar.setTabSelectedListener(this);
    }

    private ArrayList<Fragment> getFragments() {
        ArrayList<Fragment> fragments = new ArrayList<>();
        fragments.add(MainFragment.newInstance());
        fragments.add(AccountFragment.newInstance());
        return fragments;
    }

    private void setDefaultFragment() {
        FragmentManager fm = getSupportFragmentManager();
        FragmentTransaction transaction = fm.beginTransaction();
        transaction.replace(R.id.contentMain, MainFragment.newInstance());
        transaction.commit();
    }

    @Override
    public void onTabSelected(int position) {
        FragmentManager fm = getSupportFragmentManager();
        FragmentTransaction transaction = fm.beginTransaction();
        switch (position) {
            case 0:
                if (mMainFragment == null) {
                    mMainFragment = MainFragment.newInstance();
                }
                transaction.replace(R.id.contentMain, mMainFragment);
                break;
            case 1:
                if (mAccountFragment == null) {
                    mAccountFragment = AccountFragment.newInstance();
                }
                transaction.replace(R.id.contentMain, mAccountFragment);
                break;
        }
        transaction.commit();
    }

    @Override
    public void onTabUnselected(int position) {
        if (mFragments != null) {
            if (position < mFragments.size()) {
                FragmentManager fm = getSupportFragmentManager();
                FragmentTransaction transaction = fm.beginTransaction();
                Fragment fragment = mFragments.get(position);
                transaction.remove(fragment);
                transaction.commitAllowingStateLoss();
            }
        }
    }

    @Override
    public void onTabReselected(int position) {

    }
}
